package com.ledim.app;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.multidex.MultiDex;
import android.telephony.TelephonyManager;
import com.ledim.datastatistics.LeDimAnalysisUtils;
import com.ledim.utils.FrescoUtil;
import com.letv.business.flow.PlayFlowModule.ApplicationComponent;
//import com.squareup.leakcanary.LeakCanary;
//import com.squareup.leakcanary.RefWatcher;
import java.util.UUID;

public class LeDimApplication extends Application {

    private static LeDimApplication instance;
    private CrashHandler mCrashHandler;
    //private RefWatcher refWatcher;

    private static  ApplicationComponent component;
    public static LeDimApplication getInstance() {
        if (instance == null) {
            instance = new LeDimApplication();
        }

        return instance;
    }
    public static ApplicationComponent getComponentInstance() {
        if (component == null){
            component = ApplicationComponent.Initializer.init(getInstance());
        }
        return component;
    }
    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();
        // 注册全局异常捕获
        mCrashHandler = CrashHandler.getInstance();
        mCrashHandler.init(this);
        FrescoUtil.init(getApplicationContext(), 100);
        // refWatcher = LeakCanary.install(this);

        LeDimAnalysisUtils.initAnalysis(this);
    }

    public static String getDeviceId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
        String deviceId = tm.getDeviceId();

        final String tmDevice, tmSerial, androidId;
        tmDevice = "" + tm.getDeviceId();
        tmSerial = "" + tm.getSimSerialNumber();
        androidId = "" +
                android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

        UUID deviceUuid = new UUID(androidId.hashCode(),
                ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
        String uniqueId = deviceUuid.toString();

        return deviceId;
    }

    /**
     * 获取app版本号
     *
     * @return
     */
    public String getAppVersionName() {
        String versionName = "";
        try {
            PackageManager pm = LeDimApplication.getInstance().getPackageManager();
            PackageInfo pi = pm.getPackageInfo(LeDimApplication.getInstance().getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "1.0";
            }
        } catch (Exception e) {
            return "1.0";
        }
        return versionName;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }
}
